import {defineConfig} from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';
import fs from 'fs';
import AutoImport from 'unplugin-auto-import/vite';
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons';

export default defineConfig({
    plugins: [
        vue(),
        createSvgIconsPlugin({
            iconDirs: [path.resolve(__dirname, 'src/assets/images/svg')],
            symbolId: 'icon-[name]',
        }),
        AutoImport({
            imports: ['vue'],
            dirs: ['./src/components/*.vue'],
            eslintrc: {
                enabled: true,
                filepath: '.eslintrc-auto-import.json',
                globalsPropValue: true,
            },
        }),
    ],
    resolve: {
        alias: {
            '@': path.resolve(__dirname, 'src'),
            '@public': path.resolve(__dirname, 'public'),
            '@components': path.resolve(__dirname, 'src/components'),
            '@assets': path.resolve(__dirname, 'src/assets'),
            '@store': path.resolve(__dirname, 'src/store'),
            '@views': path.resolve(__dirname, 'src/views'),
            '@utils': path.resolve(__dirname, 'src/utils'),
        },
    },
    server: {
        host: '0.0.0.0',
    },
    build: {
        target: 'modules',
        outDir: 'dist',
        assetsDir: 'assets', // 静态资源的根目录
        cssCodeSplit: true,
        terserOptions: {
            compress: {
                drop_console: true,
                drop_debugger: true,
                pure_funcs: ['console.log'],
            },
            output: {
                comments: true,
            },
        },
        chunkSizeWarningLimit: 1000,
        rollupOptions: {
            output: {
                chunkFileNames: 'assets/js/[name]-[hash].js',
                entryFileNames: 'assets/js/[name]-[hash].js',
                assetFileNames: (assetInfo) => {
                    if (assetInfo.name.endsWith('.js')) {
                        return 'assets/js/[name]-[hash].[ext]';
                    }
                    if (assetInfo.name.endsWith('.css')) {
                        return 'assets/css/[name]-[hash].[ext]';
                    }
                    if (['png', 'jpeg', 'jpg', 'svg'].includes(assetInfo.name.split('.').pop())) {
                        return 'assets/images/[name]-[hash].[ext]';
                    }
                    return 'assets/[name]-[hash].[ext]';
                },
                
                manualChunks (id) {
                    if (id.includes('node_modules')) {
                        return id
                            .toString()
                            .split('node_modules/')[1]
                            .split('/')[0]
                            .toString()
                    }
                },
            },
        },
    }
});
